/*
 * The MIT License
 *
 * Copyright (c) 2010, Marc Schwerdtfeger (marc@schwerdtfeger.de)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.loosix.core.test.modulemanager
{
import flexunit.framework.TestCase;

import org.loosix.core.Loosix;
import org.loosix.core.module.constant.ModuleState;
import org.loosix.core.module.errors.ModuleError;
import org.loosix.core.test.modules.mockmodules.simple.SimpleTestModuleImpl;

public class ModuleStopTest extends TestCase
    {
        private var _testModule:SimpleTestModuleImpl

        public function ModuleStopTest(methodName:String = null)
        {
            super(methodName);
        } // endConstructor

        override public function setUp():void
        {
           Loosix.loggerService.log("setUp",this);
             _testModule = new SimpleTestModuleImpl();
            super.setUp();
        } // endFunc

        override public function getTestMethodNames():Array
        {
            return ["test_stop","test_stop_restart","test_invalid_stop"]
        } // endFunc

        public function test_stop():void
        {
           Loosix.moduleManager.registerModule(_testModule);
           assertEquals("SimpleTestModuleImpl state", ModuleState.STARTED, _testModule.state);
           _testModule.stop();
            assertEquals("SimpleTestModuleImpl has no dependencies. after a stop() the state should be", ModuleState.RESOLVED,
                         _testModule.state);
        } // endFunc

        public function test_stop_restart():void
        {
           Loosix.moduleManager.registerModule(_testModule);

           _testModule.stop();
           _testModule.start();
            assertEquals("SimpleTestModuleImpl has no dependencies. after restart state should be", ModuleState.STARTED,
                         _testModule.state);
        } // endFunc

        /**
         * das modul wurde noch nicht gestartet und wird gestoppt. es sollte eine exception folgen!
         */
        public function test_invalid_stop():void
        {
            _testModule.autoStart = false;
           Loosix.moduleManager.registerModule(_testModule);

            assertEquals("SimpleTestModuleImpl with autostart = false. after regsiter state should be", ModuleState.RESOLVED,
                         _testModule.state);

            try
            {
                _testModule.stop();
                fail("Exception was not thrown!")
            }
            catch(e:ModuleError)
            {
                 assertEquals("Exception catched! ID=", ModuleError.INVALID_STATE_CHANGE,e.errorID); 
            } // endTry
        } // endFunc

        override public function tearDown():void
        {
            Loosix.loggerService.log("tearDown",this);            
            Loosix.moduleManager.unregisterModule(_testModule.interfaceName);
            super.tearDown();
        } // endFunc

    } // endClass

} // endPackage